<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul {
            padding: 0;
            margin: 0;
            list-style: none;
            width: 1000px;
            margin: auto;
            display: flex;
            flex-wrap: wrap;
        }

        li {
            width: 23%;
            margin: 1%;
            border: 1px solid #000;
            box-sizing: border-box;
            text-align: center;
        }

        img {
            width: 100%;
            height: 200px;
        }

        .page {
            width: 1000px;
            text-align: center;
            margin: auto;
            font-size: 50px;
        }

        .disabled {
            color: #999;
        }
    </style>
</head>

<body>

    <header>
        用户名: <strong>xx</strong>
        <span>退出账号</span>
    </header>

    <!-- 
    <div class="search">
        <input type="text">
        <button>搜索</button>
    </div>


    <div>
        按照价格排序
        <button class="asc">升序</button>
        <button class="desc">降序</button>
    </div> -->

    <ul>
        <!-- <li>
            <a href="">
                <img src="" alt="">
                <h3></h3>
                <p></p>
                <strong></strong>
            </a>
        </li> -->
    </ul>


    <div class="page">
        <span class="prev disabled">&lt;</span>
        <span class="next">&gt;</span>
    </div>


</body>

</html>


<script src="../js/tools.js"></script>

<script>

    let size = 3;
    let page = 1;
    let pageAll ;

    getData()




    const oNext = document.querySelector('.next');
    const oPrev = document.querySelector('.prev') ;

    oNext.onclick = function () {
        if(this.classList.contains('disabled')) {
            return ;
        }

        page++;
        getData() ;

        if(oPrev.classList.contains('disabled')) {
            oPrev.classList.remove('disabled') ;
        }

        if(page == pageAll) {
            this.classList.add('disabled') ;
        }
       
    }


    oPrev.onclick = function () {
        if(this.classList.contains('disabled')) {
            return ;
        }

        page--;
        getData() ;

        if(oNext.classList.contains('disabled')) {
            oNext.classList.remove('disabled') ;
        }

        if(page == 1) {
            this.classList.add('disabled') ;
        }
       
    }



    function getData() {
        ajax({
            type: 'get',
            data: {
                size,
                page
            },
            path: '../php/page.php',
            success: res => {
                console.log(res);
                const { status, msg, list, pageLen } = res;
                if (status) {
                    // 页数不能整除   向上取整
                    pageAll = Math.ceil(pageLen / size) ;
                    let html = '';
                    list.forEach(v => {
                        const {
                            goods_img,
                            goods_title,
                            goods_des,
                            goods_price
                        } = v;
                        html += `
                        <li>
                            <a href="">
                                <img src="${goods_img}" alt="">
                                <h3>${goods_title}</h3>
                                <p>${goods_des}</p>
                                <strong>${goods_price}</strong>
                            </a>
                        </li>
                        `
                        const oUl = document.querySelector('ul');
                        oUl.innerHTML = html;
                    })
                }
            }
        })
    }

</script>